/**
 * 浅拷贝 只对第一层键值进行复制
 * @param {*} obj1 
 * @param {*} obj2 
 */
function shallowCopy(obj1,obj2){
    for(var key in obj1){
        obj2[key]=obj1[key]
    }
}
let obj1 = {
    a: '1',
    b: '2',
    c: {
      name: 'Demi'
    }
  }
let obj2 = {}
shallowCopy(obj1, obj2) //将obj1的数据拷贝到obj2
obj2.c.name = 'dingFY'
console.log(obj1) // {a: "1", b: "2", c: {name: 'dingFY'}}
console.log(obj2) // {a: "1", b: "2", c: {name: 'dingFY'}}

/**
 * 深拷贝 开辟新的地址
 * @param {*} obj 
 * @returns 
 */
function deepCopy(obj){
    // 根据obj类型判断是新建一个数组还是对象
    let newObj=Array.isArray(obj)?[]:{};
    // 如果obj存在且为对象
    if(obj&&typeof obj==='object'){
        for(let key in obj){
            // 如果obj的子元素是对象，就递归
            if(obj[key]&&typeof obj[key]==='object'){
                newObj[key]=deepCopy(obj[key])
            }else{
            // 如果子元素不是对象，就直接赋值
            newObj[key]=obj[key]
            }
        }
    }
    return newObj;
}
let obj3 = {
    a: '1',
    b: '2',
    c: {
      name: 'Demi'
    }
  }
let obj4 = deepCopy(obj3) //将obj1的数据拷贝到obj2
 obj4.c.name = 'dingFY'
 console.log(obj3) // {a: "1", b: "2", c: {name: 'Demi'}}
 console.log(obj4) // {a: "1", b: "2", c: {name: 'dingFY'}}
let obj5=[1,2,3];
let obj6=deepCopy(obj5);
console.log(obj6);